package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchaseBiddingPlanDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.PurchaseBiddingPlan;
import com.guodu.entity.PurchasebiddingStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchaseBiddingPlanService;
import com.guodu.vo.PurchaseBiddingPlanQueryVo;
import com.guodu.vo.PurchaseBiddingPlanVo;

@Service("purchaseBiddingPlanService")
public class PurchaseBiddingPlanServiceImpl implements PurchaseBiddingPlanService {

	@Resource
	private PurchaseBiddingPlanDao purchaseBiddingPlanDao;

	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;

	@Override
	public long findSizeByVo(PurchaseBiddingPlanQueryVo pbpq, Units units) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanDao.findSizeByVo(pbpq, units);
	}

	@Override
	public List<PurchaseBiddingPlanVo> findByVo(
			PurchaseBiddingPlanQueryVo pbpq, Units units, int curPage,
			int pageSize) {
		// TODO Auto-generated method stub
		List<PurchaseBiddingPlan> list = purchaseBiddingPlanDao.findByVo(pbpq,units, curPage, pageSize);
		List<PurchaseBiddingPlanVo> purchaseBiddingPlanVos = new ArrayList<PurchaseBiddingPlanVo>();
		for(PurchaseBiddingPlan purchaseBiddingPlan : list ){
			PurchaseBiddingPlanVo purchaseBiddingPlanVo = new PurchaseBiddingPlanVo(purchaseBiddingPlan);
			UserInfo userInfo = userInfoDao.findById(purchaseBiddingPlan.getCreatePeople());
			if (userInfo.getUnitsId()!=null) {
				Units unitsInfos = unitsDao.findById(userInfo.getUnitsId());
				purchaseBiddingPlanVo.setIsComplete(unitsInfos.getIsComplete());
			if (unitsInfos.getIsComplete()!=1&&unitsInfos.getUnitsLevel()==1) {
				Units unitsInfo = unitsDao.findById(purchaseBiddingPlan.getUnitsId());
				purchaseBiddingPlanVo.setUnitsName(unitsInfo.getUnitsName());
				purchaseBiddingPlanVo.setIndustry(unitsInfo.getIndustry());
				
				Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
				purchaseBiddingPlanVo.setUnitsLevel(createUnits.getUnitsLevel());
				
				if(unitsInfo.getFunitsId()!=null){
					Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
					purchaseBiddingPlanVo.setFunitsName(funitsInfo.getUnitsName());
				}
			}else if(unitsInfos.getIsComplete()!=1&&unitsInfos.getUnitsLevel()==2){
				
					Units unitsInfo = unitsDao.findById(purchaseBiddingPlan.getUnitsId());
					Units funits = unitsDao.findById(unitsInfo.getFunitsId());
					purchaseBiddingPlanVo.setUnitsName(unitsInfo.getUnitsName());
					purchaseBiddingPlanVo.setFunitsName(funits.getUnitsName());
					purchaseBiddingPlanVo.setIndustry(unitsInfo.getIndustry());
					Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
					purchaseBiddingPlanVo.setUnitsLevel(createUnits.getUnitsLevel());
					
			}else if(unitsInfos.getIsComplete()==1&&purchaseBiddingPlan.getUnitsId()==null){
				purchaseBiddingPlanVo.setUnitsLevel((long)1);
			}else if(unitsInfos.getIsComplete()==1&&purchaseBiddingPlan.getUnitsId()!=null){
				Units unitsInfo = unitsDao.findById(purchaseBiddingPlan.getUnitsId());
				purchaseBiddingPlanVo.setUnitsLevel(unitsInfos.getUnitsLevel());
				purchaseBiddingPlanVo.setIndustry(unitsInfo.getIndustry());
				if (unitsInfo.getFunitsId()!=null) {
					Units funits = unitsDao.findById(unitsInfo.getFunitsId());
					purchaseBiddingPlanVo.setUnitsName(unitsInfo.getUnitsName());
					purchaseBiddingPlanVo.setFunitsName(funits.getUnitsName());
				}else{
					Units funits = unitsDao.findById(unitsInfo.getUnitsId());
					purchaseBiddingPlanVo.setFunitsName(funits.getUnitsName());
				}
			}
			}else{
				Long id = null;
				purchaseBiddingPlanVo.setUnitsLevel(id);
				if (purchaseBiddingPlan.getUnitsId()!=null&&!"".equals(purchaseBiddingPlan.getUnitsId())) {
					Units unitsInfo = unitsDao.findById(purchaseBiddingPlan.getUnitsId());
					purchaseBiddingPlanVo.setIndustry(unitsInfo.getIndustry());
					if (unitsInfo.getFunitsId()!=null) {
						Units funits = unitsDao.findById(unitsInfo.getFunitsId());
						purchaseBiddingPlanVo.setUnitsName(unitsInfo.getUnitsName());
						purchaseBiddingPlanVo.setFunitsName(funits.getUnitsName());
					}else{
						Units funits = unitsDao.findById(unitsInfo.getUnitsId());
						purchaseBiddingPlanVo.setFunitsName(funits.getUnitsName());
					}
				}
				
			}
			purchaseBiddingPlanVo.setUserName(userInfo.getUserName());
			purchaseBiddingPlanVos.add(purchaseBiddingPlanVo);
		}
		return purchaseBiddingPlanVos;
	}

	@Override
	public void save(PurchaseBiddingPlan purchaseBiddingPlan) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanDao.save(purchaseBiddingPlan);
	}

	@Override
	public PurchaseBiddingPlan findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanDao.findById(purchaseId);
	}

	@Override
	public void update(PurchaseBiddingPlan purchaseBiddingPlan,Long status) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanDao.update(purchaseBiddingPlan);
		List<PurchasebiddingStatus> entities = new ArrayList<PurchasebiddingStatus>();
		PurchasebiddingStatus entity = new PurchasebiddingStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchaseBiddingPlan.getCreatePeople());
		entity.setPurchaseId(purchaseBiddingPlan.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchaseBiddingPlanDao.changeStatus(entities);
	}

	@Override
	public List<PurchasebiddingStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasebiddingStatus> entities = new ArrayList<PurchasebiddingStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasebiddingStatus entity = new PurchasebiddingStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchaseBiddingPlanDao.changeStatus(entities);
		purchaseBiddingPlanDao.examine(ids,status);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		purchaseBiddingPlanDao.delByIds(delIds);
	}

	@Override
	public List<PurchaseBiddingPlan> findAllByVo(
			PurchaseBiddingPlanQueryVo pbpq, Units attribute) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanDao.findAllByVo(pbpq,attribute);
	}

	@Override
	public PurchaseBiddingPlanVo findAllMoney(
			PurchaseBiddingPlanQueryVo pbpq, Units units) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlanDao.findAllMoney(pbpq, units);
	}

	
	public void examinAll(PurchaseBiddingPlanQueryVo pbpq, Units units) {
		
		 purchaseBiddingPlanDao.examinAll(pbpq,units);
	}
	

	

	
	

}
